max_cont=mean(template(:))+2*std(template(:));
min_cont=mean(template(:))-2*std(template(:));

hf = figure;
ha = axes('position',[0 0 1 1]);

tmp=(template-min_cont)/(max_cont-min_cont);
tmp(tmp>1)=1;
tmp(tmp<0)=0;

t_mask=zeros(size(template));
bv_mask=zeros(size(template));

for ind=1:length(ROIs)
    t_mask(ROIs(ind).indices)=1;
    bv_mask(bv.indices)=1;
end

overlay(:,:,1)=tmp;
overlay(:,:,2)=bwperim(t_mask);
overlay(:,:,3)=bwperim(bv_mask);
hi=imagesc(overlay);
hold on
for ind=1:length(ROIs)
    [txt_x,txt_y]=ind2sub(size(template),min(ROIs(ind).indices));
    ht(ind)=text(txt_y,txt_x-10,num2str(ind),'color','w','fontweight','bold','fontsize',12);
end
box off
axis image
axis off
